/**
  ******************************************************************************
  * @file    app_alarm.c
  * @author  Sherlock
  * @version V0.1
  * @date    09/18/2024
  * @brief   Alarm handling.
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	APP_GLOBALS_ALARM
#define	APP_GLOBALS_ALARM
#include <includes.h>
#include "alarm_timing.h"


static void Alarm_GlobalRAMReset();
static void Alarm_LocalRAMReset();
static uint8_t Alarm_TimingLayerHandle(OS_FLAGS os_value);


void App_Task_Alarm(void *p_arg)
{
    uint8_t  os_err, status = DEF_FALSE;
	OS_FLAGS os_value;
	
	(void)p_arg;

	/* RAM Reset */
    Alarm_GlobalRAMReset();
  	Alarm_LocalRAMReset();

	/* HW Interface Initial */
	// status |= Alarm_HWInit();

	/* Wait for Start action finish */
	// OSSemPend(App_Sem_STARTSYNC,0,&os_err);   //等待某个任务先进行并释放信号量，其它任务才能开始运行

  	/* Wait for every task ready */
  	OSTimeDlyHMSM(0, 0, 0, 500);

  	/* HW start to work */
	// status |= Alarm_HWEnable();

	/* Initial fault detection */

	/*  Alarm control flow */
 	while (DEF_TRUE)
    {
        /*  Wait for State Update output Flags  */
		os_value = OSFlagPend(App_Flags_Alarm,
							OSALARMFlags_ALL,
							OS_FLAG_WAIT_SET_ANY + OS_FLAG_CONSUME,
							0,
							&os_err);
		if(os_err!=OS_ERR_NONE)
		SYS_DebugERROR;                                       /* Error- Only for Debug */

		/*  Workload running */
		status = DEF_FALSE;

		if(os_value&OSACADFlags_TIMING)
			status |= Alarm_TimingLayerHandle(os_value);

        //if(status) 这部分逻辑还得修改
            //SetOSFlagsForNewInform(App_Flags_WORKFLOWCHILD, OSWORKFLOWCHILDFlags_CheckAlarm);
    }
}


static void Alarm_GlobalRAMReset()
{
    Alarm_RTGlobalData = 0;
}

  	
static void Alarm_LocalRAMReset()
{
    // Alarm_InitTimingAlarmStatus(&GenFreqLowTiming);
    Alarm_InitTimingAlarmStatus(&GenVoltHighTiming);
    Alarm_InitTimingAlarmStatus(&GenCurrHighTiming);
    Alarm_InitTimingAlarmStatus(&GenAcPowerHighTiming);
    Alarm_InitTimingAlarmStatus(&GenRePowerHighTiming);
    Alarm_InitTimingAlarmStatus(&GenPFLowTiming);
    Alarm_InitTimingAlarmStatus(&SyncVoltTiming);
    Alarm_InitTimingAlarmStatus(&SyncPhaseTiming);
    Alarm_InitTimingAlarmStatus(&GenExVoltHighTiming);
    Alarm_InitTimingAlarmStatus(&GenExCurrHighTiming);
    Alarm_InitTimingAlarmStatus(&LoadVoltHighTiming);
	Alarm_InitTimingAlarmStatus(&LoadExVoltHighTiming);
    Alarm_InitTimingAlarmStatus(&LoadExCurrHighTiming);
}


static uint8_t Alarm_TimingLayerHandle(OS_FLAGS os_value)
{
	uint8_t status = DEF_FALSE;

	NominalCfg_ST TempNominalCfg;
	SeriousAlarmCfg_ST TempAlarmCfg;
    DataBase_ReadDataContent(Addr_NominalCfg_RatedPhaseVolt, (uint16_t *)&TempNominalCfg, sizeof(TempNominalCfg)/2);
    DataBase_ReadDataContent(Addr_SeriousAlarmCfg_OverVoltPhas_threshold, (uint16_t *)&TempAlarmCfg, sizeof(TempAlarmCfg)/2);
	
	// if(os_value & OSALARMFlags_GenFreqTiming)
	// {
	// 	if(Alarm_GenFreqTimingHandle())
	//         Alarm_GenFreqInputAction();
	// }
	// if(os_value & OSALARMFlags_LoadFreqTiming)
	// {
	// 	status |= Alarm_loadFreqTimingHandle();
	// }
	if(os_value & OSALARMFlags_GenVoltTiming)
	{
		if(Alarm_GenVoltTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_GenVoltInputAction();
	}
	if(os_value & OSALARMFlags_LoadVoltTiming)
	{
		if(Alarm_LoadVoltTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_LoadVoltInputAction();
	}
	if(os_value & OSALARMFlags_GenCurrTiming)
	{
		if(Alarm_GenCurrTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_GenCurrInputAction();
	}
	if(os_value & OSALARMFlags_GenExciteTiming)
	{
		if(Alarm_GenExciteTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_GenExciteInputAction();
	}
	if(os_value & OSALARMFlags_LoadExciteTiming)
	{
		if(Alarm_LoadExciteTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_LoadExciteInputAction();
	}
	if(os_value & OSALARMFlags_SyncTiming)
	{
		if(Alarm_SyncTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_SyncInputAction();
	}
	if(os_value & OSALARMFlags_PowerTiming)
	{
		if(Alarm_PowerTimingHandle(&TempNominalCfg, &TempAlarmCfg))
			status |= Alarm_PowerInputAction();
	}

	return status;
}


/*****************************************************************************
 *                                 接口函数
 *****************************************************************************/
/* 获取全体告警，用于判断当前是否存在严重故障 */
uint16_t Alarm_GetTotalAlarm(void)
{
    return Alarm_RTGlobalData;
}


/* 获取具体告警信息 */
uint8_t Alarm_GetAlarmStatus(AlarmType alarmName)
{
	uint8_t status = DEF_TRUE;
	if(Alarm_RTGlobalData & alarmName)
		status = DEF_TRUE;
	
    return status;
}


#endif	/*	APP_GLOBALS_ALARM	*/

/******************* (C) COPYRIGHT 2010 Feirui *****END OF FILE****/